<?php
class Zendvn_System_Acl{
	protected $_acl;
	protected $_role;
	
	public function __construct($aclInfo = NULL, $options = NULL){
		
		if(!empty($aclInfo)){
			$acl = new Zend_Acl();
			//Set nhom phan quyen
			$this->_role = $aclInfo['role'];
			$acl->addRole(new Zend_Acl_Role($this->_role));
			//Cap quyen
			$groupPrivileges = $aclInfo['privileges'];
			$acl->allow($this->_role,null,$groupPrivileges);
			$this->_acl = $acl;
		}
	}
	public function isAllowed($arrParam = NULL){
		$ns = new Zend_Session_Namespace('info');
		$privileges = $arrParam['module'] . '_' . $arrParam['controller'] . '_' . $arrParam['action'];
		$flag = false;
		if($this->_acl->isAllowed($this->_role,null,$privileges)){
			$flag = true;
		}
		return $flag;
	}
	public function createPrivilegeArray($opstions = null){
		$ns = new Zend_Session_Namespace('info');
		$nsInfo = $ns->getIterator();
		$group_id = $nsInfo['group']['id'];
		
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
					 ->from('privileges')
					 ->join('user_group_privileges','user_group_privileges.privilege_id=privileges.id')
					 ->where('status = ?',1,INTERGER)
					 ->where('group_id = ?',$group_id,INTEGER);
		
		$result = $db->fetchAll($select);
		if(!empty($result)){
			$arrPrivilages = array();
			foreach ($result as $key){
				$arrPrivilages[] = $key['module'] . '_' . $key['controller'] . '_' . $key['action'];
			}
		}
		$ns->acl['privileges']  = $arrPrivilages;
	}

	public function createRole($opstions = null){
		$ns = new Zend_Session_Namespace('info');
		$nsInfo = $ns->getIterator();
		$info = $nsInfo['group'];
		$group_name  = $info['group_name'];
		$ns->acl['role'] = $group_name;
	}
}